using System;
using System.Collections.Generic;
using System.IO;

namespace SharpTar
{
    public class FilesCollection : IEnumerable<TarFile>
    {
        private List<TarFile> files = new List<TarFile>();
        private TarArchive parentArchive = null;

        internal FilesCollection(TarArchive parentArchive)
        {
            this.parentArchive = parentArchive;
        }

        public FilesCollection()
        {
        }

        public TarFile this[int index]
        {
            get { return files [index]; }
            set { files [index] = value; }
        }

        public TarFile this[string name]
        {
            get
            {
                foreach(TarFile file in files)
                {
                    if(file.Header.FileName == name) return file;
                }

                throw new Exception("File not found");
            }
        }

        public void Add(TarFile file)
        {
            files.Add(file);
        }

        public void AddRange(IEnumerable<TarFile> collection)
        {
            files.AddRange(collection);
        }

        public void Remove(TarFile file)
        {
            if (parentArchive != null)
                parentArchive.anyFileRemoved = true;
            files.Remove(file);
        }

        public void RemoveAt(int index)
        {
            if (parentArchive != null)
                parentArchive.anyFileRemoved = true;
            files.RemoveAt(index);
        }

        public void RemoveRange(int index, int count)
        {
            if (parentArchive != null)
                parentArchive.anyFileRemoved = true;
            files.RemoveRange(index, count);
        }

        #region IEnumerable implementation

        public IEnumerator<TarFile> GetEnumerator()
        {
            return files.GetEnumerator();
        }

        #endregion

        #region IEnumerable implementation

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return files.GetEnumerator();
        }

        #endregion
    }
}

